//
//  FYStatusTableViewCell.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/12.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit
import SnapKit

/*
1.原创微博视图
2.转发微博视图
3.微博toolbar
*/


let StatusTableViewCellMargin: CGFloat = 10

//  自定义首页微博数据的cell
class FYStatusTableViewCell: UITableViewCell {
    
    var toolBarTopConstraint: Constraint?
    
    private lazy var originView: FYStatusOriginView = {
        let view = FYStatusOriginView()

        
        return view
        
    }()
    
    private lazy var retweetView: FYStatusRetweetView = {
        let view = FYStatusRetweetView()

        return view
    }()
    
    private lazy var toolBar: FYStatusToolBar = FYStatusToolBar()
    
    
    //  处理微博数据的ViewModel
    var statusViewModel: FYStatusViewModel? {
        didSet {
            //  设置数据
            originView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
//            print(statusViewModel?.status?.retweeted_status?.text)
            
            toolBarTopConstraint?.uninstall()
            
            //  判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.hidden = false
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                //  绑定转发微博数据
                retweetView.statusViewModel = statusViewModel
                
            } else {
            
                retweetView.hidden = true
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
                
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        originView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.equalTo(originView)
            make.trailing.equalTo(originView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //  关键的约束,contentView的底部约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
}
